package com.awenlchan.musicbox;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String[] fileList = null;
    private ListView list = null;
    private String mp3Path = null;
    private String currentMusic = null;

    private ProgressBar progress = null;
    private MusicSpy spyAgent = null;
    private ServiceConnection conn = null;
    private Handler handler = null;

    private void setProgress(){

        progress = (ProgressBar)findViewById(R.id.ProgressBar);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int max = msg.getData().getInt("max");
                int pro = msg.getData().getInt("progress");
                progress.setMax(max);
                progress.setProgress(pro);
            }
        };
        new Thread(){
            //接受service进度的线程
            //发送给UI处理器Handler
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {   e.printStackTrace();   }

                    Bundle b=new Bundle();
                    b.putInt("max",spyAgent.getMax());
                    b.putInt("progress",spyAgent.getProgress());
                    Message msg=new Message();
                    msg.setData(b);
                    handler.sendMessage(msg);
                }
            }
        }.start();
    }
    private void go(){
        mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/";
        File file = new File(mp3Path);
        fileList = file.list();
        list = (ListView)findViewById(R.id.List);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
                android.R.layout.simple_list_item_1,fileList) ;
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String music = fileList[position];

                Toast.makeText(MainActivity.this,music.replace(".mp3",""),Toast.LENGTH_SHORT).show();
                Log.i("awenlchan",music);

                currentMusic = music;

                spyAgent.setMusic(mp3Path+music);
                spyAgent.setAction("play");

            }

        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        standBind();
        setProgress();
        go();
    }

    private void standBind(){
        Intent intent = new Intent();
        intent.setClass(this,MusicService.class);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                spyAgent = (MusicSpy)service;
                Log.i("awenlchan","建立connection");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,conn,BIND_AUTO_CREATE);
    }
    public void start(View view) {
//        sendMusic("play",mp3Path+currentMusic);
        spyAgent.setMusic(mp3Path+currentMusic);
        spyAgent.setAction("play");
    }
    public void stop(View view){
//        sendMusic("stop",mp3Path+currentMusic);
        spyAgent.setMusic(mp3Path+currentMusic);
        spyAgent.setAction("stop");
    }

    public void pause(View view){
//        sendMusic("pause",mp3Path+currentMusic);
        spyAgent.setMusic(mp3Path+currentMusic);
        spyAgent.setAction("pause");
    }
}
